package linearlist;

import common.BizException;

import java.util.Arrays;

/**
 * 线性表
 *  里面有数据实际长度 和 对象数组实际长度  和ArrayList比较像
 *
 * Created by Stay on 2017/5/2  17:21.
 */
public class SequenceList<T> {

    private final static int DEFAULT_SIZE = 16;   //默认对象数组长度

    private int capacity;  //保存对象数组的长度

    private Object[] elementData;

    private int size = 0;   //实际存放对象的长度

    private final static int MAX_CAPACITY = Integer.MAX_VALUE;

    public SequenceList(){
        capacity = DEFAULT_SIZE;
        elementData[capacity] = new Object();
    }

    /**
     * 指定容量创建线性表
     *
     * @param capacity
     */
    public SequenceList(int capacity){
        if(capacity < 0){
            throw new BizException("数组大小错误: "+capacity);
        }
        elementData = new Object[capacity];
        this.capacity = capacity;
    }

    /**
     * 初始化线性表 以传入元素为首 创建线性表
     *
     * @param element
     */
    public SequenceList(T element){
        this();
        elementData[0] = element;
        size ++;
    }

    public void insert(T element){
        if(size == MAX_CAPACITY){
            throw new IndexOutOfBoundsException("线性表存储长度越界");
        }
        elementData[size] = element;
        size ++;
    }

    @SuppressWarnings("Since15")
    public void insert(T element, int index){
        if(index < 0 || size == MAX_CAPACITY){
            throw new IndexOutOfBoundsException("线性表存储索引越界");
        }
        if(size == capacity){
            capacity = capacity << 1;
            if(capacity == MAX_CAPACITY){
                throw new IndexOutOfBoundsException("线性表存储索引越界");
            }
            elementData = Arrays.copyOf(elementData,capacity);
        }
        System.arraycopy(elementData,index,elementData,index+1,size - index);
        elementData[size] = element;
        size ++;
    }

    public void remove(T element){

    }






}
